package cn.zz.veggie.application.assembler;

import cn.hutool.core.util.StrUtil;
import cn.zz.comm.util.DateUtil;
import cn.zz.veggie.core.enums.biz.OrderStatus;
import cn.zz.veggie.model.domain.Order;
import cn.zz.veggie.model.domain.OrderInfo;
import cn.zz.veggie.model.domain.Shop;
import cn.zz.veggie.model.query.OrderQuery;
import cn.zz.veggie.web.types.dto.mgmt.order.MgmtOrderCreateDto;
import cn.zz.veggie.web.types.dto.mgmt.order.MgmtOrderItemDto;
import cn.zz.veggie.web.types.dto.mgmt.order.MgmtOrderItemSaveDto;
import cn.zz.veggie.web.types.dto.mgmt.order.MgmtOrderQueryDto;
import cn.zz.veggie.web.types.dto.mgmt.order.MgmtOrderSaveDto;

/**
 * @author zhongq
 * @since 2024/5/25
 */
public class OrderAssembler {
	public static OrderQuery buildAdminQuery(MgmtOrderQueryDto dto) {
		final OrderQuery query = new OrderQuery();
		query.shopId = dto.getShopId();
		query.status = dto.getStatus();
		query.sn = dto.getSn();
		query.delManId = dto.getDelManId();
		query.deliveryTimeStart = dto.getDeliveryTimeStart();
		query.deliveryTimeEnd = dto.getDeliveryTimeEnd();
		query.sort = dto.getSort();
		return query;
	}

	public static Order createOrder(Shop shop, MgmtOrderCreateDto dto, String orderSn) {
		final Order order = new Order();
		order.setShopId(dto.getShopId());
		order.setSn(orderSn);
		order.putStatus(OrderStatus.active);
		order.setAmount(dto.calcAmount());
		order.setDeliveryTime(dto.getDeliveryTime());
		order.setAddress(shop.getAddress());
		order.setRemark(dto.getRemark());
		return order;
	}

	public static OrderInfo buildOrderItem(Order order, MgmtOrderItemSaveDto dto) {
		final OrderInfo item = createOrderItem(order, dto);
		item.setId(dto.getId());
		return item;
	}

	public static OrderInfo createOrderItem(Order order, MgmtOrderItemDto dto) {
		OrderInfo item = new OrderInfo();
		item.setOrderId(order.getId());
		item.setProductId(dto.getProductId());
		item.setQuantity(dto.getQuantity());
		item.setShipped(dto.getShipped());
		item.setPrice(dto.getQuote());
		item.setAmount(dto.calcAmount());
		item.setRemark(StrUtil.nullToEmpty(dto.getRemark()));
		item.setCreatedAt(DateUtil.getCurTimeSec());
		item.setUpdatedAt(DateUtil.getCurTimeSec());
		return item;
	}

	public static void buildUpdate(Order order, MgmtOrderSaveDto dto) {
		order.setDelManId(dto.getDelManId());
		order.setDeliveryTime(dto.getDeliveryTime());
		order.setRemark(dto.getRemark());
	}
}